package br.com.estudante.mvc.logica;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.estudante.modelo.Estudante;

public class Deletar implements Logica {

	@Override
	public void executa(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		

		EntityManagerFactory factory = Persistence.createEntityManagerFactory("Estudante");
		EntityManager manager = factory.createEntityManager();
		
		Estudante estudante = 
				manager.find(Estudante.class, Long.parseLong(request.getParameter("id")));
		
		manager.getTransaction().begin();
		
		manager.remove(estudante);
		
		manager.getTransaction().commit();
		
		manager.close();
		factory.close();
		
		RequestDispatcher rd = 
				request.getRequestDispatcher("/confirma-exclusao.jsp");
		rd.forward(request, response);
		
	}
	
}
